﻿using System;
using System.Linq;
using System.Web.Mvc;
using PMSMVC.Models;
using Webdiyer.WebControls.Mvc;

namespace PMSMVC.Controllers
{
    [Authorize]
    public class DocTaskStatisticController : Controller
    {
        PMSDBContext db = new PMSDBContext();

        //
        // GET: /DocTaskStatistic/

        public ActionResult Index(string StartTime, string StopTime, int? id)
        {
            var query = db.DocTasks
                .GroupBy(c => c.Executor)
                .Select(g => new DocTaskStatisticbyExecutorModel
                {
                    Executor = g.Key,
                    Quantity = 1.0 * g.Count(d => d.TaskStatus == "CLOSED") / g.Count(),
                    Element = g.Count(d => d.TaskStatus == "CLOSED"),
                    Total = g.Count()
                });
            if ((!string.IsNullOrEmpty(StartTime)) && (!string.IsNullOrEmpty(StopTime)))
            {
                try
                {
                    DateTime dt1 = DateTime.Parse(StartTime);
                    DateTime dt2 = DateTime.Parse(StopTime);
                    query = db.DocTasks
                        .Where(f => f.PostTime > dt1 && f.PostTime < dt2)
                        .GroupBy(c => c.Executor)
                        .Select(g => new DocTaskStatisticbyExecutorModel
                        {
                            Executor = g.Key,
                            Quantity = 1.0 * g.Count(d => d.TaskStatus == "CLOSED") / g.Count(),
                            Element = g.Count(d => d.TaskStatus == "CLOSED"),
                            Total = g.Count()
                        });
                }
                catch
                {
                    RedirectToAction("Index", "Project");
                }
            }
            PagedList<DocTaskStatisticbyExecutorModel> list = query.OrderBy(p => p.Executor).ToPagedList(id ?? 1, 10);
            return View(list);
        }

    }
}
